/*
 * 入库审核
 * 参数   交易码：trancode=51121
 * 
 */

package org.ccams.tran;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.ccams.domain.TMateriel;
import org.ccams.domain.TMaterielStock;
import org.ccams.domain.TpInDetail;
import org.ccams.domain.User;
import org.ccams.util.PageParameter;
import org.ccams.web.UserSession;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class Tran51121 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long num =Long.parseLong( request.getParameter("num"));//取得记录ID
		Short ischeck = Short.parseShort(request.getParameter("state"));//取得审核状态
		int uid = Integer.parseInt(request.getParameter("uid"));
		
		String params = (String) request.getParameter("detail");
		JSONArray detailParams = new JSONArray(params);
		
		//获得审核人
		HttpSession session = request.getSession();
		UserSession userSession = (UserSession)session.getAttribute("userSession");
		User user = userSession.getUser();
		String userName=user.getUserName();
		//取得当前日期
		Date adate = new Date();
		
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";	
		int k=0;		
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			TpInDetail tid = new TpInDetail();
			tid.setNum(num);
			tid.setAname(userName);
			tid.setIscheck(ischeck);
			tid.setAdate(adate);
			k = getCcamsService().getTpindetailDao().updateCommit(tid);//更新审核信息
			if(ischeck == 0 && k >= 1){
				for(int i=0;i<detailParams.length();i++){
					org.json.JSONObject detail = detailParams.optJSONObject(i);
					TMaterielStock tms = new TMaterielStock();
//					tms.setUname(uname);
					tms.setUid(uid);
//					tms.setMaterielName(detail.getString("materielName"));
					tms.setIdt_materiel(detail.getLong("idt_Materiel"));
					tms.setAmount(detail.getLong("amount"));
					
					int idt = getCcamsService().getTmaterielstockDao().getIdtStock(tms);
					if(idt == 0){//部门库存中没有此物料记录信息,执行添加
						PageParameter page = new PageParameter();
						page.setCondition(detail.getString("materielName"));
						page.setStart(0);
						page.setLimit(1);
						List<TMateriel> list =  getCcamsService().getTmaterielDao().getTMaterielByPage(page);
						TMateriel tm = list.get(0);
						
						tms.setIdt_materiel(tm.getIdt_materiel());
						tms.setAttrib1(tm.getAttrib1());
						tms.setAttrib2(tm.getAttrib2());
						tms.setAttrib3(tm.getAttrib3());
						tms.setAttrib4(tm.getAttrib4());
						tms.setAttrib5(tm.getAttrib5());
						k = getCcamsService().getTmaterielstockDao().insertTMaterielStock(tms);
					}
					else{
						k = getCcamsService().getTmaterielstockDao().updateStockAmount(tms);//更新入库数量
					}
				}
			}
		}
		catch(Exception ex){
			txm.rollback(status);
	        ex.printStackTrace();
		}finally{
			txm.commit(status);
		}
		if (k >= 1) {
			success = true;
			message = "审核成功";
		} else {
			success = false;
			message = "审核失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
